<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

use app\common\logic\Category as CommonCategory;

/**
 * 分类管理逻辑（后台）
 */
class Category extends AdminBase
{
    
    public static $commonCategoryLogic = null;
    
    /**
     * 基类初始化
     */
    public function __construct()
    {
        // 执行父类构造方法
        parent::__construct();
        
        empty(static::$commonCategoryLogic) && static::$commonCategoryLogic = new CommonCategory();
    }
    
    /**
     * 获取分类列表搜索条件
     */
    public function getWhere($data = [])
    {
        return static::$commonCategoryLogic->getWhere($data);
    }
    
    /**
     * 获取分类列表
     */
    public function getCategoryList($where = [])
    {
        return static::$commonCategoryLogic->getCategoryList($where);
    }
    
    /**
     * 获取分类信息
     */
    public function getCategoryInfo($where = [], $field = true)
    {
        return static::$commonCategoryLogic->getCategoryInfo($where, $field);
    }
    
    /**
     * 分类编辑
     */
    public function categoryEdit($data = [])
    {
        $url = url('categorylist');
        
        $validate_result = $this->validateCategory->scene('edit')->check($data);
        
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateCategory->getError()];
        }
        
        $result = static::$commonCategoryLogic->categoryEdit($data);
        
        if (is_array($result) && $result[0] == RESULT_ERROR) {
            return $result;
        }
        
        action_log('编辑', '编辑分类，id：' . ($data['id'] ?? '新增'));
        
        return [RESULT_SUCCESS, '操作成功', $url];
    }
    
    /**
     * 获取分类选择器数据
     */
    public function getCategorySelector($data = [])
    {
        return static::$commonCategoryLogic->getCategorySelector($data);
    }
    
    /**
     * 获取分类选项（用于产品编辑页面）
     */
    public function getCategoryOptions($data = [])
    {
        return static::$commonCategoryLogic->getCategoryOptions($data);
    }
    
    /**
     * 分类删除
     */
    public function categoryDel($data = [])
    {
        $url = url('categorylist');
        
        if (empty($data['id'])) {
            return [RESULT_ERROR, '参数错误'];
        }
        
        $result = static::$commonCategoryLogic->categoryDel(['id' => $data['id']]);
        
        if (is_array($result) && $result[0] == RESULT_ERROR) {
            return $result;
        }
        
        action_log('删除', '删除分类，id：' . $data['id']);
        
        return [RESULT_SUCCESS, '删除成功', $url];
    }
}